package demo.thread;

public class Dead {
	public static void main(String[] args) {
		new Thread(new MyThread(1)).start();
		new Thread(new MyThread(2)).start();
	}
}

class MyThread implements Runnable {

	static Object o1 = new Object();
	static Object o2 = new Object();
	int x;

	public MyThread(int x) {
		this.x = x;
	}

	public void run() {
		while (true) {
			if (this.x == 1) {
				synchronized (o1) {
					System.out.println("1");
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (o2) {
						System.out.println("-----1");
					}
				}
			}
			if (this.x == 2) {
				synchronized (o2) {
					System.out.println("2");
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (o1) {
						System.out.println("-----2");
					}
				}
			}
		}
	}

}
